import json


def to_json():
    """ 将python对象转换为json """
    d = {
        'name': 'py',
        'price': 14.23

    }
    result = json.dumps(d, indent=4)
    print(result)


def to_object():
    """ 将json转换为python对象 """
    json_str = """
        {
            "name":"张三",
            "age":28,
            "score":88.5,
            "is_success":"true"
        }
    """
    result = json.loads(json_str)
    print(result)
    print(result['name'])


def json_to_object_from_file():
    """ 从文件读取内容，并转换成python对象 """
    file = open('./static/book.json', 'r', encoding='utf-8')
    json_str = file.read()
    print(json_str)
    result = json.loads(json_str)
    print(result['adr'])
    file.close()


if __name__ == '__main__':
    to_json()
    # to_object()
    # json_to_object_from_file()
